package com.xca.znb.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.xca.znb.entity.User;
import com.xca.znb.model.impl.AssetsModelImpl;
import com.xca.znb.presenter.AssetsPresenter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class MyAssetsActivity extends AppCompatActivity {
    private AssetsPresenter presenter ;
    private BottomSheetDialog bottomSheetDialog ;
    private TextView Recharge  , BalanceNumber;
    private LinearLayout Payouts , Details;
    private ImageButton myWalletBack ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_wallet);
        myWalletBack = findViewById(R.id.myWallet_back_btn) ;
        Recharge = findViewById(R.id.my_wallet_balance_top_up );
        Payouts = findViewById(R.id.my_wallet_payouts);
        Details = findViewById(R.id.my_wallet_transaction_details);
        BalanceNumber = findViewById(R.id.my_wallet_balance_number);

        final EditText editText1 = new EditText(this);
        editText1.setHint("输入在此处");


        AssetsModelImpl assetsModel = new AssetsModelImpl(this) ;
        presenter = new AssetsPresenter(assetsModel);
        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");
        double account = presenter.getNum(phone);
        String loginAccount = String.valueOf(account);
        BalanceNumber.setText(loginAccount);




        myWalletBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        Recharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBottomSheetRecharge();
            }
        });
        Payouts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBottomSheetPayouts();
            }
        });

        Details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyAssetsActivity.this , BillingActivity.class);
                startActivity(intent);
            }
        });
    }


    private void  showBottomSheetRecharge(){
        bottomSheetDialog = new BottomSheetDialog(MyAssetsActivity.this);
        bottomSheetDialog.setContentView(R.layout.balance_recharge);
        EditText RechargeNumber = bottomSheetDialog.findViewById(R.id.recharge_money_number);
        ImageButton Cancel = bottomSheetDialog.findViewById(R.id.recharge_cancel) ;
        Button Recharge = bottomSheetDialog.findViewById(R.id.recharge_btn) ;

        Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomSheetDialog.dismiss();
            }
        });
        Recharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取phone
                SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
                String phone = sharedPreferences.getString("phone" , "");
                //设置账单类型
                String type = "充值" ;
                //获取当前时间：精确到秒
                LocalDateTime nowTime = LocalDateTime.now();
                String secTime = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                String dayTime = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                //获取充值金额
                double recharge_number = Double.parseDouble(RechargeNumber.getText().toString());
                if (recharge_number > 0){
                    AlertDialog.Builder builder = new AlertDialog.Builder(MyAssetsActivity.this);
                    builder.setMessage("请输入支付密码：");
                    final EditText editText = new EditText(MyAssetsActivity.this);
                    builder.setView(editText);
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            User user = presenter.getUserByPhone(phone);
                            String inputPassword = editText.getText().toString();
                            if (inputPassword.equals(user.getPay_password())){
                                presenter.insertAccount(phone , recharge_number , type , dayTime , secTime);

                                double account = presenter.getNum(phone);
                                String loginAccount = String.valueOf(account);
                                BalanceNumber.setText(loginAccount);
                                Toast.makeText(MyAssetsActivity.this, "您充值了" + recharge_number, Toast.LENGTH_SHORT).show();
                                bottomSheetDialog.dismiss();
                            }else {
                                Toast.makeText(MyAssetsActivity.this, "密码输入错误", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    builder.setNegativeButton("取消",null);
                    builder.create().show();
                }else {
                    Toast.makeText(MyAssetsActivity.this, "输入金额不规范", Toast.LENGTH_SHORT).show();
                }
            }
        });
        bottomSheetDialog.show();
    }
    private void showBottomSheetPayouts(){
        bottomSheetDialog = new BottomSheetDialog(MyAssetsActivity.this) ;
        bottomSheetDialog.setContentView(R.layout.payouts_money);
        EditText PayoutsNumber = bottomSheetDialog.findViewById(R.id.payouts_money_number);
        ImageButton Cancel = bottomSheetDialog.findViewById(R.id.payouts_cancel) ;
        Button Payouts = bottomSheetDialog.findViewById(R.id.payouts_btn) ;

        Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomSheetDialog.dismiss();
            }
        });
        Payouts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Double balance_now = Double.valueOf(BalanceNumber.getText().toString());
                Double payouts_number = Double.valueOf(PayoutsNumber.getText().toString());

                //获取phone
                SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
                String phone = sharedPreferences.getString("phone" , "");
                //设置账单类型
                String type = "提现" ;
                //获取当前时间：精确到秒
                LocalDateTime nowTime = LocalDateTime.now();
                String secTime = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                String dayTime = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

                if (balance_now >= payouts_number){
                    if (payouts_number > 0){

                        AlertDialog.Builder builder = new AlertDialog.Builder(MyAssetsActivity.this);
                        builder.setMessage("请输入支付密码：");
                        final EditText editText = new EditText(MyAssetsActivity.this);
                        builder.setView(editText);
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                User user = presenter.getUserByPhone(phone);
                                String inputPassword = editText.getText().toString();
                                if (inputPassword.equals(user.getPay_password())){
                                    presenter.insertAccount(phone , payouts_number , type , dayTime , secTime);

                                    double account = presenter.getNum(phone);
                                    String loginAccount = String.valueOf(account);
                                    BalanceNumber.setText(loginAccount);
                                    refreshPage();
                                    Toast.makeText(MyAssetsActivity.this, "您提现了" + payouts_number, Toast.LENGTH_SHORT).show();
                                    bottomSheetDialog.dismiss();
                                }else {
                                    Toast.makeText(MyAssetsActivity.this, "密码输入错误", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        builder.setNegativeButton("取消",null);
                        builder.create().show();
                    }else {
                        Toast.makeText(MyAssetsActivity.this, "数值输入不规范", Toast.LENGTH_SHORT).show();
                    }
                }
                else {
                    Toast.makeText(MyAssetsActivity.this, "余额不足", Toast.LENGTH_SHORT).show();
                }
            }
        });
        bottomSheetDialog.show();
    }


    private void refreshPage() {
        // 刷新逻辑，例如重新从数据库或网络获取数据
        // 更新UI组件
        invalidateOptionsMenu(); // 如果需要刷新菜单项
    }
    private void showPasswordDialog1() {
        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        new AlertDialog.Builder(MyAssetsActivity.this)
                .setTitle("请输支付原密码")
                .setView(input)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
                        String phone = sharedPreferences.getString("phone" , "");
                        String enteredPassword = input.getText().toString();
                        User user = presenter.getUserByPhone(phone);
                        if (enteredPassword.equals(user.getPay_password())) {

                        } else {
                            // 密码错误，显示提示
                            Toast.makeText(MyAssetsActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }
    private void showPasswordDialog2() {
        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        new AlertDialog.Builder(MyAssetsActivity.this)
                .setTitle("请输支付原密码")
                .setView(input)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
                        String phone = sharedPreferences.getString("phone" , "");
                        String enteredPassword = input.getText().toString();
                        User user = presenter.getUserByPhone(phone);
                        if (enteredPassword.equals(user.getPay_password())) {
                        } else {
                            // 密码错误，显示提示
                            Toast.makeText(MyAssetsActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }
}
